package net.pulseproject.router.app.view;

import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import net.pulseproject.commons.gui.ActionListenerX;
import net.pulseproject.commons.gui.IconsCache;
import net.pulseproject.commons.util.GuiUtil;
import net.pulseproject.router.app.model.Model;

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

public class PreferencesDialog extends JDialog {

	private static final long serialVersionUID = 1550755573020025669L;

	private final JFrame parent;
	
//	private final JTextField inpPortFilter = new JTextField();
	
//	private final transient Model model;
	
//	private final transient Binder<ModelField, String> portFilterBinder =
//		new ModelTextComponentBinder(ModelField.PORT_FILTER, this.inpPortFilter);
	
	private final IconsCache iconsCache;
	
	@Inject
	public PreferencesDialog(
			final Model model,
			final IconsCache iconsCache,
			@Assisted final JFrame parent
			) {
		super(parent);
		this.iconsCache = iconsCache;
		if(parent == null) { throw new IllegalArgumentException("parent == null"); }
		if(model == null) { throw new IllegalArgumentException("model == null"); }
		
		this.setTitle("Preferences");
		this.setModal(true);
		
		this.parent = parent;
//		this.model = model;
		
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(final WindowEvent e) {
				PreferencesDialog.this.onClosing();
			}
		});
		
//		this.model.addBinding(this.portFilterBinder);
		
		this.getContentPane().add(this.createMainPanel());
		this.pack();
		GuiUtil.setCenterLocation(this);
	}
	
	private JPanel createMainPanel() {
		final JPanel panel = new JPanel();
		// MINOR when more preferences available soon => use GridBagLayout instead
		
//		final JLabel lblPortFilter = new JLabel("Port Filter RegExp:");
//		panel.add(lblPortFilter);
//		this.inpPortFilter.setToolTipText("For example: \"IAC Driver - .*\"");
//		this.inpPortFilter.setColumns(20);
//		panel.add(this.inpPortFilter);
		
		final JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListenerX(this.iconsCache, this.parent, "Could not close preferences dialog!") {
			@Override protected void action(final ActionEvent event) {
				PreferencesDialog.this.onClosing();
		}});
		this.getRootPane().setDefaultButton(btnClose);
		panel.add(btnClose);
		
		return panel;
	}

	protected final void onClosing() {
//		this.model.removeBinding(this.portFilterBinder);
//		this.model.setPortFilter(this.inpPortFilter.getText());
		
		this.dispose();
	}
	
}
